//
//  RegisterViewController.m
//  sklive
//
//  Created by Macx on 2017/4/26.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "RegisterViewController.h"

#import "NSString+Common.h"
#import "CountDownTool.h"
#import "PerMessageDetailsViewController.h"

@interface RegisterViewController () <UITextFieldDelegate>

@end

@implementation RegisterViewController{
    UITextField *_userName;
    UITextField *_code;
    UITextField *_passWord;
    UITextField *_againPassWord;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self buildUI];
    
}

-(void)popAction{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark ==========构建UI==========
- (void)buildUI {
    
    UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [returnBtn setImage:IMAGE(@"return") forState:UIControlStateNormal];
    [returnBtn addTarget:self action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];
    returnBtn.frame = CGRectMake(0, 0, 44, 44);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:returnBtn];
    

    self.navigationItem.title = @"注册";
    self.view.backgroundColor = kWhiteColor;
    
    NSArray *imgArr = @[@"icon_reg_num", @"icon_reg_pwd", @"icon_reg_pwd", @"icon_reg_code"];
    NSArray *placeArr = @[@"请输入手机号", @"请设置密码", @"请确认密码", @"请输入验证码"];
    CGSize size = [LZCTools textSizeWithText:@"测字数" font:kUHSystemFontWithSize(14)];
//    for (int i = 0; i < imgArr.count; i++) {
//        
//        UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(30, 64+SW(8)+SW(8)*i, SCREEN_WIDTH-60, size.height)];
//        [self.view addSubview:field];
//        field.delegate = self;
//        field.tag = 11+i;
//        field.placeholder = placeArr[i];
//        field.textColor = kLightGrayColor;
//        field.clearButtonMode = UITextFieldViewModeWhileEditing;
//        field.font = kUHSystemFontWithSize(14);
//        [field setValue:kLightGrayColor forKeyPath:@"_placeholderLabel.textColor"];
//        field.leftViewMode=UITextFieldViewModeAlways;
//        UIImageView *img = [[UIImageView alloc] initWithImage:IMAGE(imgArr[i])];
//        UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, img.width+10, field.height)];
//        [vv addSubview:img];
//        field.leftView = vv;
//        
//        if (i == 1 || i == 2) {
//            field.secureTextEntry = YES;
//        }
////        else
////            field.keyboardType = UIKeyboardTypeNumberPad;
//        
//        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(20, SW(8)+size.height+5+64+SW(8)*i, SCREEN_WIDTH-40, 1)];
//        [self.view addSubview:line];
//        line.backgroundColor = kLightGrayColor;
//        line.tag = 21+i;
//        
//    }
//    
//    _userName = [self.view viewWithTag:11];
//    _passWord = [self.view viewWithTag:12];
//    _againPassWord = [self.view viewWithTag:13];
//    _code = [self.view viewWithTag:14];
    
//    UIView *line = [self.view viewWithTag:24];
    
    
	_userName = [[UITextField alloc] initWithFrame:CGRectMake(30, Height_NavBar+30, SCREEN_WIDTH-60, size.height)];
	[self.view addSubview:_userName];
	_userName.delegate = self;
	_userName.placeholder = placeArr[0];
	_userName.textColor = kLightGrayColor;
	_userName.clearButtonMode = UITextFieldViewModeWhileEditing;
	_userName.font = kUHSystemFontWithSize(14);
	[_userName setValue:kLightGrayColor forKeyPath:@"_placeholderLabel.textColor"];
	_userName.leftViewMode=UITextFieldViewModeAlways;
	UIImageView *imgV = [[UIImageView alloc] initWithImage:IMAGE(imgArr[0])];
	UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imgV.width+10, _userName.height)];
	[vv addSubview:imgV];
	_userName.leftView = vv;
	
	
	UIView *line0 = [[UIView alloc] initWithFrame:CGRectMake(20, _userName.y+_userName.height+5, SCREEN_WIDTH-40, 1)];
	[self.view addSubview:line0];
	line0.backgroundColor = kLightGrayColor;
	
	_passWord = [[UITextField alloc] initWithFrame:CGRectMake(30, line0.y+line0.height+15, SCREEN_WIDTH-60, size.height)];
	[self.view addSubview:_passWord];
	_passWord.delegate = self;
	_passWord.placeholder = placeArr[1];
	_passWord.textColor = kLightGrayColor;
	_passWord.clearButtonMode = UITextFieldViewModeWhileEditing;
	_passWord.font = kUHSystemFontWithSize(14);
	[_passWord setValue:kLightGrayColor forKeyPath:@"_placeholderLabel.textColor"];
	_passWord.leftViewMode=UITextFieldViewModeAlways;
	UIImageView *imgV2 = [[UIImageView alloc] initWithImage:IMAGE(imgArr[1])];
	UIView *vv2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imgV2.width+10, _passWord.height)];
	[vv2 addSubview:imgV2];
	_passWord.leftView = vv2;
	
	UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(20, _passWord.y+_passWord.height+5, SCREEN_WIDTH-40, 1)];
	[self.view addSubview:line1];
	line1.backgroundColor = kLightGrayColor;
	
	_againPassWord = [[UITextField alloc] initWithFrame:CGRectMake(30, line1.y+line1.height+15, SCREEN_WIDTH-60, size.height)];
	[self.view addSubview:_againPassWord];
	_againPassWord.delegate = self;
	_againPassWord.placeholder = placeArr[2];
	_againPassWord.textColor = kLightGrayColor;
	_againPassWord.clearButtonMode = UITextFieldViewModeWhileEditing;
	_againPassWord.font = kUHSystemFontWithSize(14);
	[_againPassWord setValue:kLightGrayColor forKeyPath:@"_placeholderLabel.textColor"];
	_againPassWord.leftViewMode=UITextFieldViewModeAlways;
	UIImageView *imgV3 = [[UIImageView alloc] initWithImage:IMAGE(imgArr[2])];
	UIView *vv3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imgV3.width+10, _passWord.height)];
	[vv3 addSubview:imgV3];
	_againPassWord.leftView = vv3;
	
	UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(20, _againPassWord.y+_againPassWord.height+5, SCREEN_WIDTH-40, 1)];
	[self.view addSubview:line2];
	line2.backgroundColor = kLightGrayColor;
	
	_code = [[UITextField alloc] initWithFrame:CGRectMake(30, line2.y+line2.height+15, SCREEN_WIDTH-60, size.height)];
	[self.view addSubview:_code];
	_code.delegate = self;
	_code.placeholder = placeArr[3];
	_code.textColor = kLightGrayColor;
	_code.clearButtonMode = UITextFieldViewModeWhileEditing;
	_code.font = kUHSystemFontWithSize(14);
	[_code setValue:kLightGrayColor forKeyPath:@"_placeholderLabel.textColor"];
	_code.leftViewMode=UITextFieldViewModeAlways;
	UIImageView *imgV4 = [[UIImageView alloc] initWithImage:IMAGE(imgArr[3])];
	UIView *vv4 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imgV4.width+10, _passWord.height)];
	[vv4 addSubview:imgV4];
	_code.leftView = vv4;
	
	UIView *line3 = [[UIView alloc] initWithFrame:CGRectMake(20, _code.y+_code.height+5, SCREEN_WIDTH-40, 1)];
	[self.view addSubview:line3];
	line3.backgroundColor = kLightGrayColor;
    
    UIButton *codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:codeBtn];
    [codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    codeBtn.titleLabel.font = kUHSystemFontWithSize(12);
    [codeBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [codeBtn addTarget:self action:@selector(getCodeClick:) forControlEvents:UIControlEventTouchUpInside];
    CGSize codeSize = [LZCTools textSizeWithText:codeBtn.titleLabel.text font:kUHSystemFontWithSize(13)];
    codeBtn.layer.cornerRadius = 3;
    codeBtn.layer.masksToBounds = YES;
    codeBtn.backgroundColor = UIMainColorRGB;
    codeBtn.frame = CGRectMake(SCREEN_WIDTH-codeSize.width-50, _code.centerY-codeSize.height/2-3, codeSize.width+20, codeSize.height+6);
    
    
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:registerBtn];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    CGFloat btnHeight = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(14)].height+SW(20);
    registerBtn.frame = CGRectMake(20, line3.y+1+20, SCREEN_WIDTH-40, btnHeight);
    registerBtn.backgroundColor = UIMainColorRGB;
    registerBtn.layer.cornerRadius = 5;
    registerBtn.layer.masksToBounds = YES;
    [registerBtn addTarget:self action:@selector(registerClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *agreement1 = [[UILabel alloc] init];
    [self.view addSubview:agreement1];
    agreement1.text = @"登录即代表您同意";
    agreement1.textAlignment = NSTextAlignmentCenter;
    agreement1.textColor = kLightGrayColor;
    agreement1.alpha = 0.8;
    agreement1.font = kUHSystemFontWithSize(12);
    UILabel *agreement2 = [[UILabel alloc] init];
    [self.view addSubview:agreement2];
    agreement2.text = @"星河汇隐私政策";
    agreement2.textAlignment = NSTextAlignmentCenter;
    agreement2.textColor = UIMainColorRGB;
    agreement2.font = kUHSystemFontWithSize(12);
    // 下划线
    NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:agreement2.text attributes:attribtDic];
    //赋值
    agreement2.attributedText = attribtStr;
    [agreement2 addTarget:self action:@selector(agreementClick:)];
    
    CGSize agreement1Size = [LZCTools textSizeWithText:agreement1.text font:agreement1.font];
    CGSize agreement2Size = [LZCTools textSizeWithText:agreement2.text font:agreement2.font];
    
    agreement1.frame = CGRectMake((SCREEN_WIDTH-(agreement1Size.width+agreement2Size.width))/2, SCREEN_HEIGHT-SW(20)-agreement1Size.height, agreement1Size.width, agreement1Size.height);
    agreement2.frame = CGRectMake(agreement1.x+agreement1Size.width, SCREEN_HEIGHT-SW(20)-agreement1Size.height, agreement2Size.width, agreement2Size.height);

    
    UILabel *haveAccount = [[UILabel alloc] init];
    [self.view addSubview:haveAccount];
    haveAccount.textColor = kLightGrayColor;
    // 不同颜色
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"已有账号？登录"];
    NSRange redRange = NSMakeRange([[string string] rangeOfString:@"登录"].location, [[string string] rangeOfString:@"登录"].length);
    [string addAttribute:NSForegroundColorAttributeName value:UIMainColorRGB range:redRange];
    [haveAccount setAttributedText:string];
    [haveAccount sizeToFit];
    
    haveAccount.font = kUHSystemFontWithSize(12);
    [haveAccount addTarget:self action:@selector(returnLoginClick)];

    CGSize haveAccountSize = [LZCTools textSizeWithText:haveAccount.text font:haveAccount.font];
    haveAccount.frame = CGRectMake(SCREEN_WIDTH-20-haveAccountSize.width, registerBtn.y+registerBtn.height+10, haveAccountSize.width, haveAccountSize.height);
    
    
}

#pragma mark ===========响应方法==========
/**
 获取验证码
 */
- (void)getCodeClick:(id)sender {
    NSLog(@"获取验证码");
    
     // 手机号不为空
     if ([NSString isEmpty:_userName.text]) {
     [self presentViewController:[LZCTools alertMessage:@"手机号不可为空"] animated:YES completion:nil];
     return;
     }
     // 手机号码输入验证
     if (![NSString isValidatePhoneNumber:_userName.text]) {
     [self presentViewController:[LZCTools alertMessage:@"您输入的不是手机号"] animated:YES completion:nil];
     return;
     }
    
    [[NetworkTool sharedTool] requestWithName:@"获取手机验证码" URLString:APIMobileCodes parameters:@{@"mobile":_userName.text} method:@"GET" callBack:^(id responseObject) {
    }];
    
    [CountDownTool timeCountDown:sender second:60];
}

/**
 注册请求
 */
- (void)registerClick:(id)sender {
    NSLog(@"点击注册响应");
    
    
    // 手机号不为空
    if ([NSString isEmpty:_userName.text]) {
        [self presentViewController:[LZCTools alertMessage:@"手机号不可为空"] animated:YES completion:nil];
        return;
    }
    // 手机号码输入验证
    if (![NSString isValidatePhoneNumber:_userName.text]) {
        [self presentViewController:[LZCTools alertMessage:@"您输入的不是手机号"] animated:YES completion:nil];
        return;
    }
    // 密码不为空
    if ([NSString isEmpty:_passWord.text]) {
        [self presentViewController:[LZCTools alertMessage:@"密码不可为空"] animated:YES completion:nil];
        return;
    }
    // 密码长度要大于6
    if (_passWord.text.length < 6) {
        [self presentViewController:[LZCTools alertMessage:@"密码不可以少于6位数"] animated:YES completion:nil];
        return;
    }
	
	// 确认密码与密码需相同
	if (![_passWord.text isEqualToString:_againPassWord.text]) {
		[self presentViewController:[LZCTools alertMessage:@"两次输入的密码不一致，请检查"] animated:YES completion:nil];
		return;
	}
	
    // 验证码不为空
    if ([NSString isEmpty:_code.text]) {
        [self presentViewController:[LZCTools alertMessage:@"验证码不可为空"] animated:YES completion:nil];
        return;
    }
    
    NSDictionary *param = @{@"mobile":_userName.text,
                            @"password":_passWord.text,
                            @"mobile_code":_code.text};
    
    [[NetworkTool sharedTool] requestWithName:@"手机注册" URLString:APIRegisteredMobile parameters:param method:@"POST" callBack:^(id responseObject) {
        [self returnLoginClick];
    }];
    
    
}
/**
 用户协议
 */
- (void)agreementClick:(id)sender {
    NSLog(@"点击用户协议");
//    [self.navigationController pushViewController:[[PerMessageDetailsViewController alloc] init] animated:YES];
    PerMessageDetailsViewController *detailVC = [[PerMessageDetailsViewController alloc] init];
    detailVC.type = 1;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}
/**
 返回登录页
 */
- (void)returnLoginClick {
    NSLog(@"返回登录响应");
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark ===========代理方法==========
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
